package com.anjiplus.template.gaea.business.modules.quartz.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.db.ds.simple.SimpleDataSource;
import com.anjiplus.template.gaea.business.modules.quartz.util.DataSourceUtil;
import com.anjiplus.template.gaea.business.modules.quartz.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

;

@Slf4j
@RestController
@RequestMapping(path = "/YyGsController")
public class YyGsController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    /**
     * 读取yy信息
     *
     * @param param
     * @return
     */
    @PostMapping("/getYYcoment")
    public Result getYYcoment(@RequestBody String param) {
        try {
            String selectSQL = "SELECT\n" +
                    "  ORG.NAME AS NAME,\n" +
                    "\tORG.CODE AS yyCode,\n" +
                    "\tO.CODE AS yyztCode,\n" +
                    "\t'' as gsCode,\n" +
                    "\t'' as gsztCdoe,\n" +
                    "\t'' as kmtx,\n" +
                    "\tO.PK_RELORG as yyorgPK,\n" +
                    "\tO.NAME AS yyztName,\n" +
                    "\tO.PK_ACCBOOK as yyztPK,\n" +
                    "\tO.PK_SETOFBOOK AS ztlxPK,\n" +
                    "\tOS.CODE AS ZTLXCODE,\n" +
                    "\tOS.NAME AS ZTLXNAME,\n" +
                    "\t0 as delete_state\n" +
                    "FROM\n" +
                    "\tORG_ACCBOOK  O  \n" +
                    "\tLEFT JOIN ORG_SETOFBOOK OS ON O.PK_SETOFBOOK = OS.PK_SETOFBOOK\n" +
                    "\tLEFT JOIN ORG_ORGS ORG ON O.PK_RELORG = ORG.PK_ORG";
            Session oracleSession = DataSourceUtil.getOracleSession();
            List<Entity> query = oracleSession.query(selectSQL);
            int success = 0;
            if (query.size() > 0) {
                for (Map<String, Object> gSmap : query) {
                    // 判断是否存在
                    String isYou = "select * from yy_org_orgs where yyztCode = '" + gSmap.get("yyztCode") + "'";
                    List<Map<String, Object>> maps = jdbcTemplate.queryForList(isYou);
                    if (maps.size() > 0) {
                        System.out.println("已存在，不需抓取！！！" + gSmap.get("yyztCode"));
                    } else {
                        String insertSql = "INSERT INTO `yy_org_orgs` (\n" +
                                "`NAME`,\n" +
                                "`yyCode`,\n" +
                                "`yyztCode`,\n" +
                                "`gsCode`,\n" +
                                "`gsztCode`,\n" +
                                "`kmtx`,\n" +
                                "`yyorgPK`,\n" +
                                "`yyztName`,\n" +
                                "`yyztPK`,\n" +
                                "`ztlxPK`,\n" +
                                "`ZTLXCODE`,\n" +
                                "`ZTLXNAME`,\n" +
                                "`delete_state` \n" +
                                ")\n" +
                                "VALUES\n" +
                                "\t(\n" +
                                "\t'" + gSmap.get("NAME") + "',\n" +
                                "\t'" + gSmap.get("yyCode") + "',\n" +
                                "\t'" + gSmap.get("yyztCode") + "',\n" +
                                "\t'" + gSmap.get("gsCode") + "',\n" +
                                "\t'" + gSmap.get("gsztCode") + "',\n" +
                                "\t'" + gSmap.get("kmtx") + "',\n" +
                                "\t'" + gSmap.get("yyorgPK") + "',\n" +
                                "\t'" + gSmap.get("yyztName") + "',\n" +
                                "\t'" + gSmap.get("yyztPK") + "',\n" +
                                "\t'" + gSmap.get("ztlxPK") + "',\n" +
                                "\t'" + gSmap.get("ZTLXCODE") + "',\n" +
                                "\t'" + gSmap.get("ZTLXNAME") + "',\n" +
                                "\t'" + gSmap.get("delete_state") + "' \n" +
                                "\t);";
                        int update = jdbcTemplate.update(insertSql);
                        if (update > 0) {
                            success++;
                        }
                    }
                }
            }
            return Result.success("读取成功：" + success);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 添加
     *
     * @param param
     * @return
     */
    @PostMapping("/addyygs")
    public Result addTask(@RequestBody Map<String, Object> param) {
        DataSource ds = new SimpleDataSource(jdbcUrl, username, password);
        try (Session session = Session.create(ds)) {
            if ("oracle".equals(databaseType)) {
                Entity entity = Entity.create("yy_org_orgs");
                entity.set("ID", String.valueOf(IdUtil.getSnowflakeNextId()).substring(11));
                entity.set("delete_state", 0);
                for (Map.Entry<String, Object> stringObjectEntry : param.entrySet()) {
                    entity.set(stringObjectEntry.getKey(), stringObjectEntry.getValue());
                }
                session.insert(entity);
            } else {
                String insertSql = "INSERT INTO `yy_org_orgs` (\n" +
                        "`NAME`,\n" +
                        "`yyCode`,\n" +
                        "`yyztCode`,\n" +
                        "`gsCode`,\n" +
                        "`gsztCode`,\n" +
                        "`kmtx`,\n" +
                        "`yyorgPK`,\n" +
                        "`yyztName`,\n" +
                        "`yyztPK`,\n" +
                        "`ztlxPK`,\n" +
                        "`ZTLXCODE`,\n" +
                        "`ZTLXNAME`,\n" +
                        "`delete_state` \n" +
                        ")\n" +
                        "VALUES\n" +
                        "\t(\n" +
                        "\t'" + param.get("NAME") + "',\n" +
                        "\t'" + param.get("yyCode") + "',\n" +
                        "\t'" + param.get("yyztCode") + "',\n" +
                        "\t'" + param.get("gsCode") + "',\n" +
                        "\t'" + param.get("gsztCode") + "',\n" +
                        "\t'" + param.get("kmtx") + "',\n" +
                        "\t'" + param.get("yyorgPK") + "',\n" +
                        "\t'" + param.get("yyztName") + "',\n" +
                        "\t'" + param.get("yyztPK") + "',\n" +
                        "\t'" + param.get("ztlxPK") + "',\n" +
                        "\t'" + param.get("ZTLXCODE") + "',\n" +
                        "\t'" + param.get("ZTLXNAME") + "',\n" +
                        "\t'0' \n" +
                        "\t);";
                jdbcTemplate.update(insertSql);
            }
            return Result.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param param
     * @return
     */
    @PostMapping("/updateyygs")
    public Result updateyygs(@RequestBody Map<String, Object> param) {

        DataSource ds = new SimpleDataSource(jdbcUrl, username, password);
        try (Session session = Session.create(ds)) {
            if ("oracle".equals(databaseType)) {
                Entity entity = Entity.create("yy_org_orgs");
                for (Map.Entry<String, Object> stringObjectEntry : param.entrySet()) {
                    entity.set(stringObjectEntry.getKey(), stringObjectEntry.getValue());
                }
                session.update(entity, Entity.create("yy_org_orgs").set("id", param.get("id")));
            } else {
                String updateSql = "UPDATE `yy_org_orgs` \n" +
                        "SET `NAME` = '" + param.get("NAME") + "',\n" +
                        "`yyCode` = '" + param.get("yyCode") + "',\n" +
                        "`yyztCode` = '" + param.get("yyztCode") + "',\n" +
                        "`gsCode` = '" + param.get("gsCode") + "',\n" +
                        "`gsztCode` = '" + param.get("gsztCode") + "',\n" +
                        "`kmtx` = '" + param.get("kmtx") + "',\n" +
                        "`yyorgPK` = '" + param.get("yyorgPK") + "',\n" +
                        "`yyztName` = '" + param.get("yyztName") + "',\n" +
                        "`yyztPK` = '" + param.get("yyztPK") + "',\n" +
                        "`ztlxPK` = '" + param.get("ztlxPK") + "',\n" +
                        "`ZTLXCODE` = '" + param.get("ZTLXCODE") + "',\n" +
                        "`ZTLXNAME` = '" + param.get("ZTLXNAME") + "'\n" +
                        "WHERE\n" +
                        "\t`id` = '" + param.get("id") + "';";
                jdbcTemplate.update(updateSql);
            }
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteyygs")
    public Result deleteyygs(@RequestParam("id") String id) {
        DataSource ds = new SimpleDataSource(jdbcUrl, username, password);
        try (Session session = Session.create(ds)) {
            if ("oracle".equals(databaseType)) {
                Entity entity = Entity.create("yy_org_orgs");
                entity.set("delete_state", 1);
                session.update(entity, Entity.create("yy_org_orgs").set("id", id));
            } else {
                String updateSql = "UPDATE `yy_org_orgs` \n" +
                        "SET  `delete_state` = '1'\n" +
                        "WHERE\n" +
                        "\t`id` = '" + id + "';";
                jdbcTemplate.update(updateSql);
            }
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询列表
     *
     * @param param
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> param) {
        try {
            String condition = "";
            // 拼接条件
//            if (!ObjectUtils.isEmpty(param.get("name"))) {
//                condition += " and ";
//            }else if(!ObjectUtils.isEmpty(param.get("name"))){
//                condition += " and ";
//            }
            List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from yy_org_orgs where delete_state = 0 " + condition);
            return Result.success(mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @PostMapping("/getById")
    public Result getById(@RequestParam("id") Integer id) {
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap("select * from yy_org_orgs where delete_state = 0 and id = ?", id);
            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

}
